package org.cid54.android.jshopper.dao.impl;

import android.os.AsyncTask;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.ShoppingListDao;
import org.cid54.android.jshopper.data.CsvFetcher;
import org.cid54.android.jshopper.data.impl.CsvFetcherImpl;
import org.cid54.android.jshopper.model.ShoppingList;

/**
 *
 * @author fgo
 */
public class ShoppingListDaoImpl implements ShoppingListDao {

    private final String shoppingListsSubUrl = "shoppingLists";
    private final String shoppingListsSubUrlDelete = "delete";
    private CsvFetcher fetcher = new CsvFetcherImpl();

    public void getShoppingLists(final DaoCallBackMethod<List<ShoppingList>> callback) {

        AsyncTask<Void, Void, List<ShoppingList>> task = new AsyncTask<Void, Void, List<ShoppingList>>() {

            @Override
            protected List<ShoppingList> doInBackground(Void... arg0) {

                List<ShoppingList> resultL = new ArrayList<ShoppingList>();

                List<String[]> strings = fetcher.getData(shoppingListsSubUrl);

                for (String[] line : strings) {

                    ShoppingList s = new ShoppingList();

                    s.setId(Long.parseLong(line[0]));
                    s.setName(line[1]);

                    resultL.add(s);

                }

                return resultL;
            }

            @Override
            protected void onPostExecute(List<ShoppingList> result) {
                super.onPostExecute(result);
                callback.execute(result);
            }
        };

        task.execute();

    }

    public void saveShoppingList(final ShoppingList shoppingList, final DaoCallBackMethod<Boolean> callBack) {
        AsyncTask<ShoppingList, Void, Boolean> task = new AsyncTask<ShoppingList, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(ShoppingList... shoppingLists) {

                ShoppingList sl = shoppingLists[0];

                Map<String, String> argsMap = new HashMap<String, String>();

                argsMap.put("name", sl.getName());
                argsMap.put("usergroup", sl.getUserGroupName());

                return fetcher.postData(shoppingListsSubUrl, argsMap);

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                callBack.execute(result);
            }
        };

        task.execute(shoppingList);
    }

    public void deleteShoppingList(ShoppingList shoppingList, final DaoCallBackMethod<Void> callBack) {

        AsyncTask<ShoppingList, Void, Boolean> task = new AsyncTask<ShoppingList, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(ShoppingList... shoppingLists) {

                ShoppingList sl = shoppingLists[0];

                return fetcher.postData(shoppingListsSubUrl + "/" + sl.getId() + "/" + shoppingListsSubUrlDelete);

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                callBack.execute(null);
            }
        };

        task.execute(shoppingList);
    }
}
